home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-poosiz.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  101 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                     S Y S T E M . P O O L _ S I Z E                      --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Storage_Pools;    use System.Storage_Pools;
  27. with System.Storage_Elements; use System.Storage_Elements;
  28. with System.Address_To_Access_Conversions;
  29.  
  30. package body System.Pool_Size is
  31.  
  32.    package SC is new Address_To_Access_Conversions (Storage_Count);
  33.  
  34.    ------------------
  35.    -- Storage_Size --
  36.    ------------------
  37.  
  38.    function  Storage_Size (Pool : Stack_Bounded_Pool) return Storage_Count is
  39.    begin
  40.       return Pool.Pool_Size;
  41.    end Storage_Size;
  42.  
  43.    --------------
  44.    -- Allocate --
  45.    --------------
  46.  
  47.    procedure Allocate
  48.      (Pool         : in out Stack_Bounded_Pool;
  49.       Address      : out System.Address;
  50.       Storage_Size : Storage_Count;
  51.       Alignment    : Storage_Count)
  52.    is
  53.    begin
  54.       if Pool.First_Free /= 0 then
  55.          Address := Pool.The_Pool (Pool.First_Free)'Address;
  56.          Pool.First_Free := SC.To_Pointer (Address).all;
  57.  
  58.       elsif
  59.         Pool.First_Empty <= (Pool.Pool_Size - Pool.Aligned_Elmt_Size + 1)
  60.       then
  61.          Address := Pool.The_Pool (Pool.First_Empty)'Address;
  62.          Pool.First_Empty := Pool.First_Empty + Pool.Aligned_Elmt_Size;
  63.  
  64.       else
  65.          raise Storage_Error;
  66.       end if;
  67.    end Allocate;
  68.  
  69.    ----------------
  70.    -- Deallocate --
  71.    ----------------
  72.  
  73.    procedure Deallocate
  74.      (Pool         : in out Stack_Bounded_Pool;
  75.       Address      : System.Address;
  76.       Storage_Size : Storage_Count;
  77.       Alignment    : Storage_Count)
  78.    is
  79.    begin
  80.       SC.To_Pointer (Address).all := Pool.First_Free;
  81.       Pool.First_Free := Address - Pool.The_Pool'Address + 1;
  82.    end Deallocate;
  83.  
  84.    ----------------
  85.    -- Initialize --
  86.    ----------------
  87.  
  88.    procedure Initialize  (Pool : in out Stack_Bounded_Pool) is
  89.       Align : constant Storage_Count := Pool.Alignment;
  90.  
  91.    begin
  92.       Pool.First_Free := 0;
  93.       Pool.First_Empty := 1;
  94.  
  95.       --  Compute the size to allocate given the size of the element and the
  96.       --  possible Alignment clause
  97.  
  98.       Pool.Aligned_Elmt_Size := ((Pool.Elmt_Size + Align - 1) / Align) * Align;
  99.    end Initialize;
  100. end System.Pool_Size;
  101.